home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / TARFILE.GZ / tarfile / libtiff / tools / ras2tiff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  7.7 KB  |  264 lines

  1. /* $Header: /usr/people/sam/tiff/tools/RCS/ras2tiff.c,v 1.29 1996/01/10 19:35:30 sam Rel $ */
  2.  
  3. /*
  4.  * Copyright (c) 1988-1996 Sam Leffler
  5.  * Copyright (c) 1991-1996 Silicon Graphics, Inc.
  6.  *
  7.  * Permission to use, copy, modify, distribute, and sell this software and 
  8.  * its documentation for any purpose is hereby granted without fee, provided
  9.  * that (i) the above copyright notices and this permission notice appear in
  10.  * all copies of the software and related documentation, and (ii) the names of
  11.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  12.  * publicity relating to the software without the specific, prior written
  13.  * permission of Sam Leffler and Silicon Graphics.
  14.  * 
  15.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  16.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  17.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  18.  * 
  19.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  20.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  21.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  23.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  24.  * OF THIS SOFTWARE.
  25.  */
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <ctype.h>
  31.  
  32. #include "rasterfile.h"
  33. #include "tiffio.h"
  34.  
  35. #define    howmany(x, y)    (((x)+((y)-1))/(y))
  36. #define    streq(a,b)    (strcmp(a,b) == 0)
  37. #define    strneq(a,b,n)    (strncmp(a,b,n) == 0)
  38.  
  39. #ifndef BINMODE
  40. #define    BINMODE
  41. #endif
  42.  
  43. static    uint16 compression = (uint16) -1;
  44. static    int jpegcolormode = JPEGCOLORMODE_RGB;
  45. static    int quality = 75;        /* JPEG quality */
  46. static    uint16 predictor = 0;
  47.  
  48. static void usage(void);
  49. static    int processCompressOptions(char*);
  50.  
  51. int
  52. main(int argc, char* argv[])
  53. {
  54.     unsigned char* buf;
  55.     uint32 row;
  56.     tsize_t linebytes, scanline;
  57.     TIFF *out;
  58.     FILE *in;
  59.     struct rasterfile h;
  60.     uint16 photometric;
  61.     uint16 config = PLANARCONFIG_CONTIG;
  62.     uint32 rowsperstrip = (uint32) -1;
  63.     int c;
  64.     extern int optind;
  65.     extern char* optarg;
  66.  
  67.     while ((c = getopt(argc, argv, "c:r:")) != -1)
  68.         switch (c) {
  69.         case 'c':        /* compression scheme */
  70.             if (!processCompressOptions(optarg))
  71.                 usage();
  72.             break;
  73.         case 'r':        /* rows/strip */
  74.             rowsperstrip = atoi(optarg);
  75.             break;
  76.         case '?':
  77.             usage();
  78.             /*NOTREACHED*/
  79.         }
  80.     if (argc - optind != 2)
  81.         usage();
  82.     in = fopen(argv[optind], "r" BINMODE);
  83.     if (in == NULL) {
  84.         fprintf(stderr, "%s: Can not open.\n", argv[optind]);
  85.         return (-1);
  86.     }
  87.     if (fread(&h, sizeof (h), 1, in) != 1) {
  88.         fprintf(stderr, "%s: Can not read header.\n", argv[optind]);
  89.         return (-2);
  90.     }
  91.     if (h.ras_magic != RAS_MAGIC) {
  92.         fprintf(stderr, "%s: Not a rasterfile.\n", argv[optind]);
  93.         return (-3);
  94.     }
  95.     out = TIFFOpen(argv[optind+1], "w");
  96.     if (out == NULL)
  97.         return (-4);
  98.     TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (uint32) h.ras_width);
  99.     TIFFSetField(out, TIFFTAG_IMAGELENGTH, (uint32) h.ras_height);
  100.     TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
  101.     TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, h.ras_depth > 8 ? 3 : 1);
  102.     TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, h.ras_depth > 1 ? 8 : 1);
  103.     TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);
  104.     if (h.ras_maptype != RMT_NONE) {
  105.         uint16* red;
  106.         register uint16* map;
  107.         register int i, j;
  108.         int mapsize;
  109.  
  110.         buf = (unsigned char *)_TIFFmalloc(h.ras_maplength);
  111.         if (buf == NULL) {
  112.             fprintf(stderr, "No space to read in colormap.\n");
  113.             return (-5);
  114.         }
  115.         if (fread(buf, h.ras_maplength, 1, in) != 1) {
  116.             fprintf(stderr, "%s: Read error on colormap.\n",
  117.                 argv[optind]);
  118.             return (-6);
  119.         }
  120.         mapsize = 1<<h.ras_depth; 
  121.         if (h.ras_maplength > mapsize*3) {
  122.             fprintf(stderr,
  123.                 "%s: Huh, %d colormap entries, should be %d?\n",
  124.                 argv[optind], h.ras_maplength, mapsize*3);
  125.             return (-7);
  126.         }
  127.         red = (uint16*)_TIFFmalloc(mapsize * 3 * sizeof (uint16));
  128.         if (red == NULL) {
  129.             fprintf(stderr, "No space for colormap.\n");
  130.             return (-8);
  131.         }
  132.         map = red;
  133.         for (j = 0; j < 3; j++) {
  134. #define    SCALE(x)    (((x)*((1L<<16)-1))/255)
  135.             for (i = h.ras_maplength/3; i-- > 0;)
  136.                 *map++ = SCALE(*buf++);
  137.             if ((i = h.ras_maplength/3) < mapsize) {
  138.                 i = mapsize - i;
  139.                 _TIFFmemset(map, 0, i*sizeof (uint16));
  140.                 map += i;
  141.             }
  142.         }
  143.         TIFFSetField(out, TIFFTAG_COLORMAP,
  144.              red, red + mapsize, red + 2*mapsize);
  145.         photometric = PHOTOMETRIC_PALETTE;
  146.         if (compression == (uint16) -1)
  147.             compression = COMPRESSION_PACKBITS;
  148.         TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
  149.     } else {
  150.         /* XXX this is bogus... */
  151.         photometric = h.ras_depth == 24 ?
  152.             PHOTOMETRIC_RGB : PHOTOMETRIC_MINISBLACK;
  153.         if (compression == (uint16) -1)
  154.             compression = COMPRESSION_LZW;
  155.         TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
  156.     }
  157.     switch (compression) {
  158.     case COMPRESSION_JPEG:
  159.         if (photometric == PHOTOMETRIC_RGB && jpegcolormode == JPEGCOLORMODE_RGB)
  160.             photometric = PHOTOMETRIC_YCBCR;
  161.         TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);
  162.         TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
  163.         break;
  164.     case COMPRESSION_LZW:
  165.     case COMPRESSION_DEFLATE:
  166.         if (predictor != 0)
  167.             TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);
  168.         break;
  169.     }
  170.     TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric);
  171.     linebytes = ((h.ras_depth*h.ras_width+15) >> 3) &~ 1;
  172.     scanline = TIFFScanlineSize(out);
  173.     if (scanline > linebytes) {
  174.         buf = (unsigned char *)_TIFFmalloc(scanline);
  175.         _TIFFmemset(buf+linebytes, 0, scanline-linebytes);
  176.     } else
  177.         buf = (unsigned char *)_TIFFmalloc(linebytes);
  178.     TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
  179.         TIFFDefaultStripSize(out, rowsperstrip));
  180.     for (row = 0; row < h.ras_height; row++) {
  181.         if (fread(buf, linebytes, 1, in) != 1) {
  182.             fprintf(stderr, "%s: scanline %lu: Read error.\n",
  183.                 argv[optind], (unsigned long) row);
  184.             break;
  185.         }
  186.         if (h.ras_type == RT_STANDARD && h.ras_depth == 24) {
  187.             tsize_t cc = h.ras_width;
  188.             unsigned char* cp = buf;
  189. #define    SWAP(a,b)    { unsigned char t = (a); (a) = (b); (b) = t; }
  190.             do {
  191.                 SWAP(cp[0], cp[2]);
  192.                 cp += 3;
  193.             } while (--cc);
  194.         }
  195.         if (TIFFWriteScanline(out, buf, row, 0) < 0)
  196.             break;
  197.     }
  198.     (void) TIFFClose(out);
  199.     return (0);
  200. }
  201.  
  202. static int
  203. processCompressOptions(char* opt)
  204. {
  205.     if (streq(opt, "none"))
  206.         compression = COMPRESSION_NONE;
  207.     else if (streq(opt, "packbits"))
  208.         compression = COMPRESSION_PACKBITS;
  209.     else if (strneq(opt, "jpeg", 4)) {
  210.         char* cp = strchr(opt, ':');
  211.         if (cp && isdigit(cp[1]))
  212.             quality = atoi(cp+1);
  213.         if (cp && strchr(cp, 'r'))
  214.             jpegcolormode = JPEGCOLORMODE_RAW;
  215.         compression = COMPRESSION_JPEG;
  216.     } else if (strneq(opt, "lzw", 3)) {
  217.         char* cp = strchr(opt, ':');
  218.         if (cp)
  219.             predictor = atoi(cp+1);
  220.         compression = COMPRESSION_LZW;
  221.     } else if (strneq(opt, "zip", 3)) {
  222.         char* cp = strchr(opt, ':');
  223.         if (cp)
  224.             predictor = atoi(cp+1);
  225.         compression = COMPRESSION_DEFLATE;
  226.     } else
  227.         return (0);
  228.     return (1);
  229. }
  230.  
  231. char* stuff[] = {
  232. "usage: ras2tiff [options] input.ras output.tif",
  233. "where options are:",
  234. " -r #        make each strip have no more than # rows",
  235. "",
  236. " -c lzw[:opts]    compress output with Lempel-Ziv & Welch encoding",
  237. " -c zip[:opts]    compress output with deflate encoding",
  238. " -c jpeg[:opts]compress output with JPEG encoding",
  239. " -c packbits    compress output with packbits encoding",
  240. " -c none    use no compression algorithm on output",
  241. "",
  242. "JPEG options:",
  243. " #        set compression quality level (0-100, default 75)",
  244. " r        output color image as RGB rather than YCbCr",
  245. "For example, -c jpeg:r:50 to get JPEG-encoded RGB data with 50% comp. quality",
  246. "",
  247. "LZW and deflate options:",
  248. " #        set predictor value",
  249. "For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
  250. NULL
  251. };
  252.  
  253. static void
  254. usage(void)
  255. {
  256.     char buf[BUFSIZ];
  257.     int i;
  258.  
  259.     setbuf(stderr, buf);
  260.     for (i = 0; stuff[i] != NULL; i++)
  261.         fprintf(stderr, "%s\n", stuff[i]);
  262.     exit(-1);
  263. }
  264.